package com.goresor.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.goresor.dao.IGoResorJdbcDao;

@Service("customUserDetailsService")
public class CustomUserDetailsServiceImpl implements IUserService {

	@Autowired
	private IGoResorJdbcDao jdbcDao;

	@Override
	public void createUser(String username, String password, String email) {
		jdbcDao.createUser(username, password, email);
	}

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
		String googleUserId = username.substring(username.indexOf("id=")+3);
		UserDetails user = jdbcDao.getUser(googleUserId);
		
		if(user==null){
			throw new UsernameNotFoundException("Impossible de trouver l'utilisateur !");
		}
		else{
			return user;
		}
	}

}
